package DAOs;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

import Beans.Bieding;
import Beans.Veiling;
import Database.DBmanager;

public class BiedingDAO {
	//Create
	private String sqlBiedingCreate = "INSERT INTO APP.BIEDINGEN VALUES(?, ?, ?, ?, ?)";
	
	//Get
	private String sqlBiedingGetByGebruiker   = "SELECT * FROM APP.BIEDINGEN WHERE userID = ?";
	private String sqlBiedingGetByVeiling = "SELECT * FROM APP.BIEDINGEN WHERE veilingID = ?";
	
	//Delete
	private String sqlBiedingDelete = "DELETE FROM APP.BIEDINGEN where biedingID = ?";
	
	private Connection con = null;
	private PreparedStatement psCreate = null;
	private PreparedStatement psReadByGebruiker = null;
	private PreparedStatement psReadByVeiling = null;
	private PreparedStatement psDelete = null;
	
	public BiedingDAO(){
		DBmanager myDb = DBmanager.getInstance();
		con = myDb.getConnection();
		
		try{
			this.psCreate = con.prepareStatement(sqlBiedingCreate);
			this.psReadByGebruiker   = con.prepareStatement(sqlBiedingGetByGebruiker);
			this.psReadByVeiling   = con.prepareStatement(sqlBiedingGetByVeiling);
			this.psDelete = con.prepareStatement(sqlBiedingDelete);
			Statement stmt = con.createStatement();
			
			stmt.close();
		} catch (SQLException se){
			printSQLException(se);			
		}
	}
	
	public LinkedList<Bieding> getAllBiedingenFromUser(String user){
		LinkedList<Bieding> biedingenFromUser = new LinkedList<Bieding>();
		
		try {
			ResultSet rs = psReadByGebruiker.executeQuery();
			while(rs.next()){
				int biedingID 	= rs.getInt(1);
				int veilingID 	= rs.getInt(2);
				int userID		= rs.getInt(3);
				int bodHoogte	= rs.getInt(4);
				long   biedingDate	= rs.getLong(5);
				
				Bieding biedingBean = new Bieding();
				biedingBean.setAll(biedingID, veilingID, userID, bodHoogte, biedingDate);
				biedingenFromUser.add(biedingBean);
			}
			rs.close();
		} catch(SQLException se){
			printSQLException(se);
		}
		return biedingenFromUser;	
	}
	
	public LinkedList<Bieding> getAllBiedingenFromVeiling(String veiling){
		LinkedList<Bieding> biedingenFromVeiling = new LinkedList<Bieding>();
		
		try {
			ResultSet rs = psReadByVeiling.executeQuery();
			while(rs.next()){
				String biedingID 	= rs.getString(1);
				String veilingID 	= rs.getString(2);
				String userID		= rs.getString(3);
				int bodHoogte	= rs.getInt(4);
				long  biedingDate	= rs.getLong(5);
				
				Bieding biedingBean = new Bieding();
//TODO				//biedingBean.setAll(biedingID, veilingID, userID, bodHoogte, biedingDate);
				biedingenFromVeiling.add(biedingBean);			
			}
			rs.close();
		} catch(SQLException se){
			printSQLException(se);
		}
		return biedingenFromVeiling;
	}
	
	private void printSQLException(SQLException se) {
		while (se != null) {

			System.out.print("SQLException: State:   " + se.getSQLState());
			System.out.println("Severity: " + se.getErrorCode());
			System.out.println(se.getMessage());

			se = se.getNextException();
		}
	}
}
